package ch.uzh.ifi.seal.ssqa.ui.views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import ch.uzh.ifi.seal.ssqa.metrics.keyvalue.DeveloperResponsabilityKeyValueMetric;
import ch.uzh.ifi.seal.ssqa.metrics.keyvalue.IssueSeverityKeyValueMetric;
import ch.uzh.ifi.seal.ssqa.metrics.keyvalue.IssueTypeKeyValueMetric;
import ch.uzh.ifi.seal.ssqa.model.Source;

public class PieChartPanel extends JScrollPane{
	
	JPanel innerPanel; 
	
	public PieChartPanel(){
		this.innerPanel = new JPanel();
		GridLayout layout = new GridLayout(0,2);
		this.innerPanel.setLayout(layout);
		
		this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	}
	
	public void generateComponentPie(Source source){
		this.innerPanel.removeAll();
	    this.setBackground(Color.WHITE);
	    this.innerPanel.add(this.getDeveloperResponsabilityChart(source));
	    this.innerPanel.add(this.getIssueTypeChart(source));
	    this.innerPanel.add(this.getIssueSeverityChart(source));
	    this.getViewport().setView(this.innerPanel);
	}
	
	private ChartPanel getDeveloperResponsabilityChart(Source source){
		DeveloperResponsabilityKeyValueMetric metric = new DeveloperResponsabilityKeyValueMetric();
		DefaultPieDataset dataset = new DefaultPieDataset();
		HashMap<Integer, Integer> result = (HashMap<Integer, Integer>) metric.readOutData(source).getResult();
		for(Integer key : result.keySet()){
			dataset.setValue("Dev_"+key, result.get(key));
		}
		JFreeChart chart = ChartFactory.createPieChart("Developer Responsability",dataset, false, false, false);
		chart.setBorderVisible(false);
		PiePlot plot = (PiePlot) chart.getPlot();
	    plot.setSectionOutlinesVisible(false);
	    plot.setNoDataMessage("No data available");
	    ChartPanel panel = new ChartPanel(chart);
	    panel.setPreferredSize(new Dimension(250,200));
	    panel.setMaximumSize(new Dimension(250,200));
	    return panel;
	}
	
	private ChartPanel getIssueTypeChart(Source source){
		IssueTypeKeyValueMetric metric = new IssueTypeKeyValueMetric();
		DefaultPieDataset dataset = new DefaultPieDataset();
		HashMap<String, Integer> result = (HashMap<String, Integer>) metric.readOutData(source).getResult();
		for(String key : result.keySet()){
			dataset.setValue(key, result.get(key));
		}
		JFreeChart chart = ChartFactory.createPieChart("Issue Types",dataset, false, false, false);
	
		chart.setBorderVisible(false);
	    PiePlot plot = (PiePlot) chart.getPlot();
	    plot.setSectionOutlinesVisible(false);
	    plot.setNoDataMessage("No data available");
	    ChartPanel panel = new ChartPanel(chart);
	    panel.setPreferredSize(new Dimension(250,200));
	    panel.setMaximumSize(new Dimension(250,200));
	    return panel;
	}
	
	private ChartPanel getIssueSeverityChart(Source source){
		IssueSeverityKeyValueMetric metric = new IssueSeverityKeyValueMetric();
		DefaultPieDataset dataset = new DefaultPieDataset();
		HashMap<String, Integer> result = (HashMap<String, Integer>) metric.readOutData(source).getResult();
		for(String key : result.keySet()){
			dataset.setValue(key, result.get(key));
		}
		JFreeChart chart = ChartFactory.createPieChart("Issue Severities",dataset, false, false, false);
	
		chart.setBorderVisible(false);
	    PiePlot plot = (PiePlot) chart.getPlot();
	    plot.setSectionOutlinesVisible(false);
	    plot.setNoDataMessage("No data available");
	    ChartPanel panel = new ChartPanel(chart);
	    panel.setPreferredSize(new Dimension(250,200));
	    panel.setMaximumSize(new Dimension(250,200));
	    return panel;
	}
}
